package com.luo.partner.controller;

import com.luo.common.controller.BaseController;
import com.luo.common.utils.PageUtils;
import com.luo.common.utils.Query;
import com.luo.common.utils.R;
import com.luo.partner.dao.model.TabUsrs;
import com.luo.partner.dao.model.TabUsrsTemp;
import com.luo.partner.service.TabUsrsService;
import com.luo.system.domain.UserDO;
import com.luo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/partner/users")
public class TabUsrsController extends BaseController{
	@Autowired
    private TabUsrsService tabUsrsService;

	@Autowired
	UserService userService;

    @GetMapping()
    @RequiresPermissions("partner:users:list")
    String tabUsrs() {
        return "partner/users/list";
    }

    @RequestMapping("/register")
    String register() {
        return "partner/users/register";
    }

    @ResponseBody
	@GetMapping("/list")
    @RequiresPermissions("partner:users:list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
    	UserDO user = getUser();
    	List<Long> inviteUserIdList = null;
    	if (user.getLevel() == 1) {
			// 找出他下面所有合伙人的userId
    		inviteUserIdList = userService.findInviteUserId(user.getUserId());
		}else if (user.getLevel() == 2) {
			inviteUserIdList = new ArrayList<Long>();
			inviteUserIdList.add(user.getUserId());
		}
    	params.put("inviteUserIdList", inviteUserIdList);
        //查询列表数据
        Query query = new Query(params);
        List<TabUsrsTemp> tabUsrsList = tabUsrsService.findInviteUserList(query);

        int total = tabUsrsService.countInviteUserList(query);

        PageUtils pageUtils = new PageUtils(tabUsrsList, total);
        return pageUtils;
	}

	@GetMapping("/add")
    @RequiresPermissions("partner:users:add")
    String add() {
        return "partner/users/add";
    }

    @GetMapping("/edit/{usrs_id}")
    @RequiresPermissions("userEdit")
    String edit(@PathVariable("usrs_id") Long usrs_id, Model model) {
        TabUsrs tabUsrs = tabUsrsService.get(usrs_id);
        model.addAttribute("user", tabUsrs);
        return "partner/users/edit";
    }

    @GetMapping("/addRecharge/{usrs_id}")
    @RequiresPermissions("addRecharge")
    String addRecharge(@PathVariable("usrs_id") Long usrs_id, Model model) {
        TabUsrs tabUsrs = tabUsrsService.get(usrs_id);
        model.addAttribute("user", tabUsrs);
        return "partner/users/addRecharge";
    }

    /**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
    @RequiresPermissions("partner:users:add")
    public R save(TabUsrs tabUsrs) {
        if (tabUsrsService.save(tabUsrs) > 0) {
            return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
    @RequiresPermissions("userEdit")
    public R update(TabUsrs tabUsrs) {
        tabUsrsService.update(tabUsrs);
        return R.ok();
	}
	
	/**
	 * 充值，
	 */
	@ResponseBody
	@RequestMapping("/recharge")
    @RequiresPermissions("addRecharge")
    public R recharge(Long usrsId,String recharge) {
		TabUsrs usrs = tabUsrsService.get(usrsId);
		if (usrs == null) {
			return R.error("用户不存在");
		}
		BigDecimal un_settlement_recharge = null;
		try {
			un_settlement_recharge = BigDecimal.valueOf(Double.parseDouble(recharge));
			un_settlement_recharge = un_settlement_recharge.setScale(2, BigDecimal.ROUND_HALF_UP);
			usrs.setUnSettlementRecharge(usrs.getUnSettlementRecharge().add(un_settlement_recharge));
		} catch (Exception e) {
			return R.error("充值失败，充值金额有误");
		}
		
		try {
			tabUsrsService.recharge(usrs, un_settlement_recharge);
		} catch (RuntimeException e) {
			return R.error("充值失败，" + e.getMessage());
		}
		
        return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
    @RequiresPermissions("partner:users:remove")
    public R remove(String usrs_id) {
        if (tabUsrsService.remove(Long.parseLong(usrs_id)) > 0) {
            return R.ok();
        }
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
    @RequiresPermissions("partner:users:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] usrs_ids) {
        tabUsrsService.batchRemove(usrs_ids);
        return R.ok();
    }
	
}
